package cn.edu.sgu.www.mhxysy.service.baoshi.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.baoshi.BaoshiCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.baoshi.BaoshiCategory;
import cn.edu.sgu.www.mhxysy.mapper.baoshi.BaoshiCategoryMapper;
import cn.edu.sgu.www.mhxysy.service.baoshi.BaoshiCategoryService;
import cn.edu.sgu.www.mhxysy.util.ImageUpdater;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import cn.edu.sgu.www.mhxysy.util.WrapperUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class BaoshiCategoryServiceImpl implements BaoshiCategoryService {

	private final UploadUtils uploadUtils;
	private final ImageUpdater imageUpdater;
	private final BaoshiCategoryMapper baoshiCategoryMapper;

	@Autowired
	public BaoshiCategoryServiceImpl(
			UploadUtils uploadUtils,
			ImageUpdater imageUpdater,
			BaoshiCategoryMapper baoshiCategoryMapper) {
		this.uploadUtils = uploadUtils;
		this.imageUpdater = imageUpdater;
		this.baoshiCategoryMapper = baoshiCategoryMapper;
	}

	@Override
	public String upload(MultipartFile file) throws IOException {
		String directory = CustomDirectories.DIRECTORY_BS;

		return uploadUtils.upload(file, directory);
	}

	@Override
	public void insert(BaoshiCategoryDTO baoshiCategoryDTO) {
		BaoshiCategory baoshiCategory = toEntity(baoshiCategoryDTO);

		baoshiCategory.setId(null);

		baoshiCategoryMapper.insert(baoshiCategory);
	}

	@Override
	public void updateById(BaoshiCategoryDTO baoshiCategoryDTO) {
		ObjectUtils.requireNonNull(baoshiCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

		BaoshiCategory baoshiCategory = toEntity(baoshiCategoryDTO);

		baoshiCategoryMapper.updateById(baoshiCategory);
	}

	@Override
	public void updateImageById(Integer id, String image) {
		imageUpdater.updateImageById(image, new Consumer<Object>() {
			@Override
			public void accept(Object o) {
				UpdateWrapper<BaoshiCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

				baoshiCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
			}
		});
	}

	@Override
	public List<BaoshiCategory> selectAll() {
		return baoshiCategoryMapper.selectList(null);
	}

	@Override
	public BaoshiCategory selectById(Integer id) {
		return baoshiCategoryMapper.selectById(id);
	}

	@Override
	public Page<BaoshiCategory> selectByPage(Pager<BaoshiCategory> pager) {
		Page<BaoshiCategory> page = Pager.ofPage(pager);

		return baoshiCategoryMapper.selectPage(page, null);
	}

	private BaoshiCategory toEntity(BaoshiCategoryDTO baoshiCategoryDTO) {
		BaoshiCategory baoshiCategory = new BaoshiCategory();

		baoshiCategory.setId(baoshiCategoryDTO.getId());
		baoshiCategory.setName(baoshiCategoryDTO.getName());
		baoshiCategory.setNote(baoshiCategoryDTO.getNote());
		baoshiCategory.setPropertyId(baoshiCategoryDTO.getPropertyId());
		baoshiCategory.setPropertyValue(baoshiCategoryDTO.getPropertyValue());

		return baoshiCategory;
	}

}